package com.bayihouse.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bayihouse.annotation.AuthCheck;
import com.bayihouse.common.BaseResponse;
import com.bayihouse.common.CommonRequest;
import com.bayihouse.common.ErrorCode;
import com.bayihouse.common.ResultUtils;
import com.bayihouse.constant.AdministerConstant;
import com.bayihouse.exception.BusinessException;
import com.bayihouse.model.entity.Netobject;
import com.bayihouse.model.request.eqtObject.NetObjectAddRequest;
import com.bayihouse.model.vo.NetObjectVO;
import com.bayihouse.service.AdministerService;
import com.bayihouse.service.NetObjectService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/net")
@Slf4j
public class NetObjectController {

    @Resource
    private AdministerService administerService;

    @Resource
    private NetObjectService netobjectService;

    @PostMapping("/all")
    public BaseResponse<List<NetObjectVO>> getAll() {
        List<Netobject> list = netobjectService.list();

        return ResultUtils.success(netobjectService.getAdministerVOList(list));
    }

    @PostMapping("/getOne")
    @AuthCheck(mustRole = AdministerConstant.DEFAULT_ROLE)
    public BaseResponse<NetObjectVO> getOne(CommonRequest commonRequest) {
        QueryWrapper<Netobject> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", commonRequest.getId());
        Netobject one = netobjectService.getOne(queryWrapper);
        NetObjectVO netObjectVO = NetObjectVO.objToVO(one);
        return ResultUtils.success(netObjectVO);
    }


    @PostMapping("/add")
    @AuthCheck(mustRole = AdministerConstant.ADMIN_ROLE)
    public BaseResponse<Long> addOne(NetObjectAddRequest netObjectAddRequest) {
        Netobject netobject = new Netobject();
        BeanUtils.copyProperties(netObjectAddRequest, netobject);
        boolean save = netobjectService.save(netobject);
        if (!save) {
            throw new BusinessException(ErrorCode.SYSTEM_ERROR);
        }
        return ResultUtils.success(netobject.getId());
    }


}
